package com.dianzhihan.cashier.model.product;

import com.dianzhihan.cashier.controller.ErrorRedirectException;
import com.dianzhihan.cashier.mapper.ProductMapper;
import com.dianzhihan.cashier.model.AbsValidator;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Dianzhi Han
 * @version 1.0
 * @description: TODO
 * @date 2023/3/1 0:09
 */
@Slf4j
public class ProductIDValidator extends AbsValidator {
    @Override
    public String validate(String productID, String module, String redirectUrl) {
        productID = super.validate(productID, module, redirectUrl);
        return productID;
    }

    public void validate(String productId, String module, String redirectUrl, ProductMapper productMapper) {
        productId = validate(productId,module,redirectUrl);

        try{
            int productIdValue = Integer.parseInt(productId);
            int count = productMapper.selectCountById(productIdValue);
            if(count != 1){
                log.debug("{}：商品ID不存在",module);
                throw new ErrorRedirectException("商品id对应id不存在",module,redirectUrl);
            }
        }catch (NumberFormatException exc){
            log.debug("{}:商品id不是数字",module);
            throw new ErrorRedirectException("商品id必须是数字",module,redirectUrl);
        }

    }
}
